package com.qst.system.controller;

import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.core.page.TableDataInfo;
import com.qst.system.domain.vo.RiskVo;
import com.qst.system.service.IRiskService;
import com.qst.system.service.IRiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/risk")
public class RiskController extends BaseController {

    @Autowired
    private IRiskService riskService;

    /**
     * 获取所有风控数据
     */
    @PreAuthorize("@ss.hasPermi('system:risk:list')")
    @GetMapping("/list")
    public TableDataInfo getAllRisk(RiskVo  risk)  {
        startPage();
        List<RiskVo> allRiskData = riskService.getAllRiskData(risk);
        return getDataTable(allRiskData);
    }


    /**
     * 查询风控管理详细
     */
    @GetMapping("/{riskId}")
    public RiskVo getRiskDetail(@PathVariable("riskId") Integer riskId) {
        // 调用service层方法获取指定ID的风险数据
        return riskService.selectRiskById(riskId);
    }


    /**
     * 新增风控管理
     */
    @PreAuthorize("@ss.hasPermi('system:risk:add')")
    @PostMapping
    public AjaxResult addRisk(@RequestBody RiskVo riskVo) {
        try {
            // 调用service层方法新增风险数据
            riskService.insertRisk(riskVo);
            return AjaxResult.success("新增风控管理成功");
        } catch (Exception e) {
            return AjaxResult.error("新增风控管理失败：" + e.getMessage());
        }
    }
    /**
     * 修改风控管理
     */
    @PreAuthorize("@ss.hasPermi('system:risk:edit')")
    @PutMapping
    public AjaxResult updateRisk(@RequestBody RiskVo riskVo) {
        try {
            // 调用service层方法更新风险数据
            riskService.updateRisk(riskVo);
            return AjaxResult.success("修改风控管理成功");
        } catch (Exception e) {
            return AjaxResult.error("修改风控管理失败：" + e.getMessage());
        }
    }
    
    /**
     * 计算额度
     */
    @PreAuthorize("@ss.hasPermi('system:risk:calc')")
    @GetMapping("/calc/{riskId}")
    public AjaxResult calcRisk(@PathVariable("riskId") Integer riskId) {
        try {
            return riskService.calculateCreditLine(riskId);
        } catch (Exception e) {
            return AjaxResult.error("计算额度失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除风控管理
     */
    @PreAuthorize("@ss.hasPermi('system:risk:remove')")
    @DeleteMapping("/{riskIds}")
    public AjaxResult remove(@PathVariable Integer[] riskIds) {
        try {
            // 调用service层方法删除风险数据
            if (riskIds.length == 1) {
                riskService.deleteRiskById(riskIds[0]);
            } else {
                riskService.deleteRiskByIds(riskIds);
            }
            return AjaxResult.success("删除风控管理成功");
        } catch (Exception e) {
            return AjaxResult.error("删除风控管理失败：" + e.getMessage());
        }
    }

    /**
     * 查询征信
     */
    @PreAuthorize("@ss.hasPermi('system:risk:invest')")
    @GetMapping("/invest/{riskId}")
    public AjaxResult investRisk(@PathVariable("riskId") Integer riskId) {
        try {
            return riskService.investigateCredit(riskId);
        } catch (Exception e) {
            return AjaxResult.error("查询征信失败：" + e.getMessage());
        }
    }
}